﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace PDFWriter
{
    public class PDFFont : INode
    {
        public const string FONT = "Font";
        public const string OBJECT_ID = "id";
        public const string GENERATION_ID = "GenID";
        public string ReferenceID
        {
            get;
            set;
        }        
        private string xmlString = 
      "<Font id=\"4\">"+
      "<Dictionary>"+
        "<DictionaryElement type=\"name\" value=\"Type\">/Font</DictionaryElement>"+
        "<DictionaryElement type=\"name\" value=\"Subtype\">/Type1</DictionaryElement>"+
        "<DictionaryElement type=\"name\" value=\"Name\">/F1</DictionaryElement>"+
        "<DictionaryElement type=\"name\" value=\"BaseFont\">/Helvetica</DictionaryElement>"+
      "</Dictionary>"+
    "</Font>";
        
        private string ProcesXML()
        {
            string result = null;
            string generationID = null;
            if (reader.FirstChild.Name == FONT)
            {
                result = Utility.GetPDFString(reader.FirstChild.InnerXml);
                if (reader.FirstChild.Attributes[OBJECT_ID] != null)
                {
                    generationID = reader.FirstChild.Attributes[OBJECT_ID].Value;
                    if (reader.FirstChild.Attributes[GENERATION_ID] != null)
                    {
                        generationID += reader.FirstChild.Attributes[GENERATION_ID].Value;
                    }
                    else
                    {
                        generationID += " 0";
                    }

                }
                ReferenceID = generationID + " R";
                
                result = generationID + " obj\n" + result + "endobj";
            }
            return result;
        }

        private XmlDocument reader = new XmlDocument();

        public PDFFont(string xmlString)
        {
            this.xmlString = xmlString;
            reader.LoadXml(xmlString);
        }

        public PDFFont()
        {
            reader.LoadXml(xmlString);
        }

        #region INode Members

        public PDFObject GetPDFObject()
        {
            return new PDFObject() { Text = ProcesXML(),ObjectType = FONT, ObjectID= ReferenceID};
        }

        public List<PDFObject> GetPDFObjects()
        {
            return new List<PDFObject>() { new PDFObject() { Text = ProcesXML(),ObjectType = FONT, ObjectID= ReferenceID}};
        }

        #endregion
    }
}
